import { Get_Home, Add_Home, Editor_Home, Editor_home, Search_Home } from "../actionsType";

import request from "../../utils/request";
import {
    message,
} from 'antd';

const iniState = {
    Homelist: [],
    from: {
        name: '',
        province: '',
        city: '',
        county: '',
        word: '',
        price: '',
        rent: '',
        home: "",
        broker: "",
        id: '',
    },
}

const Home = (state = iniState, action) => {
    const { type, payload } = action;
    let newress = null;
    let { Homelist, from } = state;
    switch (type) {
        //获取
        case Get_Home:
            Homelist = payload;
            return {
                ...state,
                Homelist,
            };
        //搜索
        case Search_Home:
            Homelist = payload;
            return {
                ...state,
                Homelist,
            };
        //新增
        case Add_Home:
            var now = new Date();
            var year = now.getFullYear(); //得到年份
            var month = now.getMonth() + 1; //得到月份
            var date = now.getDate(); //得到日期
            var hour = now.getHours(); //得到小时数
            var minute = now.getMinutes(); //得到分钟数
            var second = now.getSeconds(); //得到秒数
            var newdate = `${year}-${month}-${date} ${hour}:${minute}:${second}`;
            newress = request({
                url: "/api/addHome",
                method: "post",
                data: {
                    arr: { ...from, newdate },
                }
            });
            newress.then(res => {
                if (res.code === 200) {
                    message.success(res.message);
                } else {
                    message.error(res.message);
                }
            })
            return {
                ...state,
            };
        //编辑
        case Editor_Home:
            now = new Date();
            year = now.getFullYear(); //得到年份
            month = now.getMonth() + 1; //得到月份
            date = now.getDate(); //得到日期
            hour = now.getHours(); //得到小时数
            minute = now.getMinutes(); //得到分钟数
            second = now.getSeconds(); //得到秒数
            newdate = `${year}-${month}-${date} ${hour}:${minute}:${second}`;
            newress = request({
                url: "/api/editorHome",
                method: "put",
                data: {
                    arr: { ...from, newdate },
                }
            });
            newress.then(res => {
                if (res.code === 200) {
                    message.success(res.message);
                } else {
                    message.error(res.message);
                }
            })
            return {
                ...state,
            };
        //受控
        case Editor_home:
            const { type, e } = payload;
            if (type) {
                from[type] = e;
            } else {
                from = {
                    name: payload.name,
                    province: payload.province,
                    city: payload.city,
                    county: payload.county,
                    word: payload.word,
                    price: payload.price,
                    rent: payload.rent,
                    home: payload.home,
                    broker: payload.broker,
                    id: payload.id,
                }
            }
            return {
                ...state,
                from,
            };
        default:
            return {
                ...state
            };
    }
}

export default Home;